package com.six.flower.service

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.github.pagehelper.PageHelper
import com.github.pagehelper.PageInfo
import com.six.flower.mapper.FlowersMapper
import com.six.flower.mapper.nameNotEmptyLike
import com.six.flower.mapper.updateWrapper
import com.six.library.model.Flowers
import com.six.library.model.ResultData
import com.six.library.model.ResultInfo
import org.apache.commons.lang.StringUtils
import org.springframework.stereotype.Service
import org.springframework.web.bind.annotation.PathVariable


/*-*- coding:utf-8 -*-
 * @Author  : debi
 * @Time    : 10/26/22
 * @Software: IntelliJ IDEA
 */
@Service
class FlowerService {
    private lateinit var flowerMapper: FlowersMapper
    fun selectById(flowers: Flowers): ResultData {
        return ResultInfo.setData(flowerMapper.selectById(flowers.id))
    }

    fun selectAll(): ResultData {
        return ResultInfo.setData(flowerMapper.findAll())
    }

    fun selectByIds(flowersIds: List<Int>): ResultData {
        return ResultInfo.setData(flowerMapper.selectBatchIds(flowersIds))
    }

    fun selectByName(
        flowerName: String?,
        name: String?,
    ): ResultData {
        val resultInfo = ResultData()
        try {
            val selectList: List<Flowers> = if (flowerName != null && StringUtils.isNotEmpty(flowerName)) {
                flowerMapper.selectList(Flowers.nameNotEmptyLike(flowerName))
            } else if (name != null && StringUtils.isNotEmpty(name)) {
                flowerMapper.selectList(Flowers.nameNotEmptyLike(name))
            } else {
                flowerMapper.findAll()
            }
            if (selectList.isEmpty()) {
                resultInfo.setCode(300).setMsg("没有数据").setData(selectList)
            } else {
                resultInfo.setCode(300).setMsg("查询成功").setData(selectList)
            }

        } catch (e: Exception) {
            resultInfo.setCode(500).setMsg("查询出错")
        }
        return resultInfo

    }

    fun selectByfLanguage(fLanguage: String): ResultData {
        return ResultInfo.setData(flowerMapper.selectList(
            QueryWrapper<Flowers>().apply {
                this.like(StringUtils.isNotEmpty(fLanguage), "flanguage", fLanguage)
            }
        ))
    }


    fun deleteBatchIds(ids: List<Int>): ResultData {
        val batchIds = flowerMapper.deleteBatchIds(ids)

        return ResultInfo.setData(batchIds)
    }

    fun updateById(flower: Flowers): ResultData {
        val batchIds = flowerMapper.update(flower, Flowers.updateWrapper(flower))

        return ResultInfo.setData(batchIds).setMsg("修改成功")
    }

    fun updateByName(flower: Flowers): ResultData {
        val batchIds = flowerMapper.update(flower, Flowers.updateWrapper(flower))
        return ResultInfo.setData(batchIds).setMsg("修改成功")
    }

    fun insert(flower: Flowers): ResultData {
        val batchIds = flowerMapper.insert(flower)
        return ResultInfo.setData(batchIds).setMsg("插入成功")
    }

    fun insertList(flowers: List<Flowers>): ResultData {
        var batchIds = 0
        flowers.stream().forEach { it ->
            val insert = flowerMapper.insert(it)
            batchIds += (insert)
        }
        return ResultInfo.setData(batchIds).setMsg("插入成功")
    }


    fun getList(
        @PathVariable(value = "pageIndex", required = false) pageIndex: Int?,
        @PathVariable(value = "pageSize", required = false) pageSize: Int?,
        @PathVariable(value = "name", required = false) name: String?,
    ): ResultData {
        var pageIndex = pageIndex
        var pageSize = pageSize
        var name = name
        val result = ResultData()
        //判断pageIndex是否是有效数据
        pageIndex = if (pageIndex == null || pageIndex < 0) 1 else pageIndex
        //判断pageSize是否是有效数据
        pageSize = if (pageSize == null || pageSize < 1) 10 else pageSize


        return try {
            // 分页查询数据
            PageHelper.startPage<Flowers>(pageIndex, pageSize)
            val list: MutableList<Flowers> = if (name == null || StringUtils.isNotEmpty(name)) {
                flowerMapper.selectList(QueryWrapper<Flowers>().apply {
                    this.like(StringUtils.isNotEmpty(name), "name", name)
                })
            } else {
                flowerMapper.findAll()
            }

            val pageInfo = PageInfo(list)
            if (pageInfo.total > 0) {
                result.setMsg("success").setData(pageInfo)
            } else {
                result.setMsg("No Data")
            }
        } catch (e: Exception) {
            result.setCode(500).setMsg("查询数据出错，请与管理员了联系");
        }

    }


}